import 'package:flutter/material.dart';

/// 文本域控件
/// Created by LY.
class TextWidget extends StatefulWidget {
  TextWidgetController controller;
  TextWidget({Key? key, required this.controller}) : super(key: key);

  @override
  _TextWidgetState createState() => _TextWidgetState();
}

class _TextWidgetState extends State<TextWidget> {
  late TextEditingController _controller;

  @override
  void initState() {
    widget.controller.getText = getText;
    _controller = TextEditingController(text: widget.controller.initText);
    widget.controller._editingController = _controller;
    super.initState();
  }

  String getText() {
    return _controller.text;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      constraints: BoxConstraints(minHeight: 140,),
      width: double.infinity,
      padding: EdgeInsets.all(8),
      child:
          TextField(
            // expands: true,
            controller: _controller,
            maxLines: null,
            minLines: 4,
            keyboardType: TextInputType.multiline,
            maxLength: 150,
            decoration: InputDecoration(hintText: "请输入描述...", border: InputBorder.none),
            onChanged: (str) {

            },
          ),
    );
  }
}
/// 文本域控件控制器
class TextWidgetController {
  String? initText;
  /// 获取文本
  late String Function() getText;

  TextEditingController? _editingController;

  /// 设置文本
  void setText(String text) {
    initText = text;
    _editingController?.text = initText ?? "";
  }
}